package cz.muni.fi.pa165.travelagency.web;
 
import com.samaxes.stripes.ejb3.EJBBean;
import cz.muni.fi.pa165.travelagency.api.entities.ICustomer;
import cz.muni.fi.pa165.travelagency.api.services.ICustomerService;
import cz.muni.fi.pa165.travelagency.dto.CustomerDTO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
@UrlBinding("/customers/{$event}/{customer.id}")
@EJB(name = "customerService", beanInterface = ICustomerService.class)
public class CustomerActionBean implements ActionBean {
 
    final static Logger log = LoggerFactory.getLogger(CustomerActionBean.class);
 
    private ActionBeanContext context;
            
    @EJBBean("java:comp/env/customerService")
    private ICustomerService service;
    
    @ValidateNestedProperties(value = {
            @Validate(on = {"create", "edit"}, field = "name", required = true, maxlength=100),
            @Validate(on = {"create", "edit"}, field = "address", required = true),
            @Validate(on = {"create", "edit"}, field = "phoneNumber", required = true, maxlength=16, mask="^(\\+)?([ \\d]){5,14}([\\d]).$")            
    })    
    private ICustomer customer;
    
    private List<ICustomer> customers;
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit-customer", "delete"})
    public void preloadCustomer() {
        String id = context.getRequest().getParameter("customer.id");
        if (id == null) {
            return;
        }
        
        customer = service.getCustomer(Long.parseLong(id));
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"all", "find"})
    public void preloadCustomers() {
        String expr = context.getRequest().getParameter("find");

        if (expr == null || expr.trim().equals(""))
            customers = service.findAll();
        else
            customers = service.findByName(expr);
    }    
        
    @DefaultHandler
    public Resolution all() {
        return new ForwardResolution("/customer_all.jsp");
    }
    
    public Resolution find() {
        return new ForwardResolution("/customer_all.jsp");
    }    
    
    @HandlesEvent("new-customer")
    public Resolution newCustomer() {
        return new ForwardResolution("/customer_create.jsp");
    }
    
    public Resolution create() {
        service.createCustomer(customer);
        return new RedirectResolution(this.getClass(), "all");
    }    
        
    public Resolution edit() {
        service.updateCustomer(customer);
        return new RedirectResolution(this.getClass(), "all");
    }   
    
    @HandlesEvent("edit-customer")
    public Resolution editCustomer() {
        if (customer == null) {
            return new RedirectResolution(this.getClass(), "all");
        }

        return new ForwardResolution("/customer_edit.jsp");
    }
    
    public Resolution delete() {
        if (customer != null) {
            service.deleteCustomer(customer);
        }
        
        return new RedirectResolution(this.getClass(), "all");
    }    

    public List<ICustomer> getAllCustomers() {
        return service.findAll();
    }
        
    public CustomerDTO getCustomer() {
        return (CustomerDTO)customer;
    }

    public void setCustomer(CustomerDTO customer) {
        this.customer = customer;
    }
   
    public List<ICustomer> getCustomers() {
        return customers;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
 
    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}
